<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<title>class SyntaxSuggest::CleanDocument - syntax_suggest: Ruby Standard Library Documentation</title>


<script src="../js/navigation.js" defer></script>
<script src="../js/search.js" defer></script>
<script src="../js/search_index.js" defer></script>
<script src="../js/searcher.js" defer></script>
<script src="../js/darkfish.js" defer></script>

<script src="../js/jquery-3.2.0.min.js"></script>

<script src="../js/vue.min.js"></script>
<script src="../js/js.cookie.min.js"></script>

<link href="../css/fonts.css" rel="stylesheet">
<link id='rdoccss' href="../css/rdoc.css" rel="stylesheet">
<link href="../css/carbon17.css" rel="stylesheet">

<script type="text/javascript">
  var rdoc_rel_prefix = "../";
  var index_rel_prefix = "../";
  var darkModeCsseHref = "../css/rdoc-dm.css"
  var defaultModeCssHref = "../css/rdoc.css"
  // var cssDarkmode = Cookies.get('darkmode');
  
  if( Cookies.get("darkmode") == "true") {
	$('#rdoccss').attr("href", darkModeCsseHref);
}

//  https://cssdeck.com/blog/simple-jquery-stylesheet-switcher/

document.write('<style type="text/css">body{display:none}</style>');

</script>


</head>
<body id="top" role="document" class="class">
  <!-- this is class.html -->

  <div id='actionbar' >
    <div class='wrapper mdiv'>
      <ul class='grids g0'></ul>
    </div> 
    <!-- VERSION HEADER for 3.3.0.preview2 NOT FOUND -->
  </div> <!-- end action bar -->

  <div class='wrapper hdiv'>

    


    <nav id='vapp' role="navigation">
    <div id="project-navigation">
      <div id="home-section" role="region" title="Quick navigation" class="nav-section">
  <h2><a href="../index.html" rel="home">Home</a></h2>

  <div id="table-of-contents-navigation"  >
    <a href="../table_of_contents.html#pages">Pages</a>
    <a href="../table_of_contents.html#classes">Classes</a>
    <a href="../table_of_contents.html#methods">Methods</a>
  </div>
</div>

      <div id="search-section" role="search" class="project-section initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <div id="search-field-wrapper">
      <input id="search-field" role="combobox" aria-label="Search"
             aria-autocomplete="list" aria-controls="search-results"
             type="text" name="search" placeholder="Search" spellcheck="false"
             title="Type to search, Up and Down to navigate, Enter to load">
    </div>

    <ul id="search-results" aria-label="Search Results"
        aria-busy="false" aria-expanded="false"
        aria-atomic="false" class="initially-hidden"></ul>
  </form>
</div>

    </div>


    

    <button id='toggleThing' @click="toggleNav()" >Show/hide navigation</button>
    <div :class="isOpen ? 'block' : 'hidden' " id='toggleMe'>
      <div id="class-metadata">
        
        
<div id="parent-class-section" class="nav-section">
  <h3>Parent</h3>

  <p class="link">Object
</div>

        
        
        
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
  <h3>Methods</h3>

  <ul class="link-list" role="directory">
    <li ><a href="#method-c-new">::new</a>
    <li ><a href="#method-i-call">#call</a>
    <li ><a href="#method-i-clean_sweep">#clean_sweep</a>
    <li ><a href="#method-i-join_consecutive-21">#join_consecutive!</a>
    <li ><a href="#method-i-join_groups">#join_groups</a>
    <li ><a href="#method-i-join_heredoc-21">#join_heredoc!</a>
    <li ><a href="#method-i-join_trailing_slash-21">#join_trailing_slash!</a>
    <li ><a href="#method-i-lines">#lines</a>
    <li ><a href="#method-i-take_while_including">#take_while_including</a>
    <li ><a href="#method-i-to_s">#to_s</a>
  </ul>
</div>

      </div>
     </div>
    </nav>


    <div id='extraz'><div class='adzbox-index'  >
      
     </div>         
    </div>

    <main role="main" aria-labelledby="class-SyntaxSuggest::CleanDocument">
    <h1 id="class-SyntaxSuggest::CleanDocument" class="class">
      class SyntaxSuggest::CleanDocument
    </h1>

    <section class="description">
    
<p>Parses and sanitizes source into a lexically aware document</p>

<p>Internally the document is represented by an array with each index containing a <a href="CodeLine.html"><code>CodeLine</code></a> correlating to a line from the source code.</p>

<p>There are three main phases in the algorithm:</p>
<ol><li>
<p>Sanitize/format input source</p>
</li><li>
<p>Search for invalid blocks</p>
</li><li>
<p>Format invalid blocks into something meaninful</p>
</li></ol>

<p>This class handles the first part.</p>

<p>The reason this class exists is to format input source for better/easier/cleaner exploration.</p>

<p>The <a href="CodeSearch.html"><code>CodeSearch</code></a> class operates at the line level so we must be careful to not introduce lines that look valid by themselves, but when removed will trigger syntax errors or strange behavior.</p>

<p>## Join Trailing slashes</p>

<p>Code with a trailing slash is logically treated as a single line:</p>

<pre>1 it &quot;code can be split&quot; \
2    &quot;across multiple lines&quot; do</pre>

<p>In this case removing line 2 would add a syntax error. We get around this by internally joining the two lines into a single “line” object</p>

<p>## Logically Consecutive lines</p>

<p>Code that can be broken over multiple lines such as method calls are on different lines:</p>

<pre>1 User.
2   where(name: &quot;schneems&quot;).
3   first</pre>

<p>Removing line 2 can introduce a syntax error. To fix this, all lines are joined into one.</p>

<p>## Heredocs</p>

<p>A heredoc is an way of defining a multi-line string. They can cause many problems. If left as a single line, Ripper would try to parse the contents as ruby code rather than as a string. Even without this problem, we still hit an issue with indentation</p>

<pre>1 foo = &lt;&lt;~HEREDOC
2  &quot;Be yourself; everyone else is already taken.&quot;&quot;
3    ― Oscar Wilde
4      puts &quot;I look like ruby code&quot; # but i&#39;m still a heredoc
5 HEREDOC</pre>

<p>If we didn’t join these lines then our algorithm would think that line 4 is separate from the rest, has a higher indentation, then look at it first and remove it.</p>

<p>If the code evaluates line 5 by itself it will think line 5 is a constant, remove it, and introduce a syntax errror.</p>

<p>All of these problems are fixed by joining the whole heredoc into a single line.</p>

<p>## Comments and whitespace</p>

<p>Comments can throw off the way the lexer tells us that the line logically belongs with the next line. This is valid ruby but results in a different lex output than before:</p>

<pre>1 User.
2   where(name: &quot;schneems&quot;).
3   # Comment here
4   first</pre>

<p>To handle this we can replace comment lines with empty lines and then re-lex the source. This removal and re-lexing preserves line index and document size, but generates an easier to work with document.</p>

    </section>

      <section id="5Buntitled-5D" class="documentation-section">





                <section id="public-class-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Public Class Methods</h3>
                </header>

                  <div id="method-c-new" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">new</span><span
                                class="method-args">(source:)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="new-source">
            <pre><span class="ruby-comment"># File syntax_suggest/clean_document.rb, line 87</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">initialize</span>(<span class="ruby-value">source:</span>)
  <span class="ruby-identifier">lines</span> = <span class="ruby-identifier">clean_sweep</span>(<span class="ruby-value">source:</span> <span class="ruby-identifier">source</span>)
  <span class="ruby-ivar">@document</span> = <span class="ruby-constant">CodeLine</span>.<span class="ruby-identifier">from_source</span>(<span class="ruby-identifier">lines</span>.<span class="ruby-identifier">join</span>, <span class="ruby-value">lines:</span> <span class="ruby-identifier">lines</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                          </section>

                <section id="public-instance-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Public Instance Methods</h3>
                </header>

                  <div id="method-i-call" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">call</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Call all of the document “cleaners” and return self</p>

                              <div class="method-source-code" id="call-source">
            <pre><span class="ruby-comment"># File syntax_suggest/clean_document.rb, line 94</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">call</span>
  <span class="ruby-identifier">join_trailing_slash!</span>
  <span class="ruby-identifier">join_consecutive!</span>
  <span class="ruby-identifier">join_heredoc!</span>

  <span class="ruby-keyword">self</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-clean_sweep" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">clean_sweep</span><span
                                class="method-args">(source:)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Remove comments</p>

<p>replace with empty newlines</p>

<pre>source = &lt;&lt;~&#39;EOM&#39;
  # Comment 1
  puts &quot;hello&quot;
  # Comment 2
  puts &quot;world&quot;
EOM

lines = CleanDocument.new(source: source).lines
expect(lines[0].to_s).to eq(&quot;\n&quot;)
expect(lines[1].to_s).to eq(&quot;puts &quot;hello&quot;)
expect(lines[2].to_s).to eq(&quot;\n&quot;)
expect(lines[3].to_s).to eq(&quot;puts &quot;world&quot;)</pre>

<p>Important: This must be done before lexing.</p>

<p>After this change is made, we lex the document because removing comments can change how the doc is parsed.</p>

<p>For example:</p>

<pre>values = LexAll.new(source: &lt;&lt;~EOM))
  User.
    # comment
    where(name: &#39;schneems&#39;)
EOM
expect(
  values.count {|v| v.type == :on_ignored_nl}
).to eq(1)</pre>

<p>After the comment is removed:</p>

<pre> values = LexAll.new(source: &lt;&lt;~EOM))
   User.

     where(name: &#39;schneems&#39;)
 EOM
 expect(
  values.count {|v| v.type == :on_ignored_nl}
).to eq(2)</pre>

                              <div class="method-source-code" id="clean_sweep-source">
            <pre><span class="ruby-comment"># File syntax_suggest/clean_document.rb, line 157</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">clean_sweep</span>(<span class="ruby-value">source:</span>)
  <span class="ruby-comment"># Match comments, but not HEREDOC strings with #{variable} interpolation</span>
  <span class="ruby-comment"># https://rubular.com/r/HPwtW9OYxKUHXQ</span>
  <span class="ruby-identifier">source</span>.<span class="ruby-identifier">lines</span>.<span class="ruby-identifier">map</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">line</span><span class="ruby-operator">|</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">line</span>.<span class="ruby-identifier">match?</span>(<span class="ruby-regexp">/^\s*#([^{].*|)$/</span>)
      <span class="ruby-identifier">$/</span>
    <span class="ruby-keyword">else</span>
      <span class="ruby-identifier">line</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-join_consecutive-21" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">join_consecutive!</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Smushes logically “consecutive” lines</p>

<pre class="ruby"><span class="ruby-identifier">source</span> = <span class="ruby-identifier">&lt;&lt;~&#39;EOM&#39;</span>
<span class="ruby-value">  User.
    where(name: &#39;schneems&#39;).
    first
</span><span class="ruby-identifier">EOM</span>

<span class="ruby-identifier">lines</span> = <span class="ruby-constant">CleanDocument</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">source:</span> <span class="ruby-identifier">source</span>).<span class="ruby-identifier">join_consecutive!</span>.<span class="ruby-identifier">lines</span>
<span class="ruby-identifier">expect</span>(<span class="ruby-identifier">lines</span>[<span class="ruby-value">0</span>].<span class="ruby-identifier">to_s</span>).<span class="ruby-identifier">to</span> <span class="ruby-identifier">eq</span>(<span class="ruby-identifier">source</span>)
<span class="ruby-identifier">expect</span>(<span class="ruby-identifier">lines</span>[<span class="ruby-value">1</span>].<span class="ruby-identifier">to_s</span>).<span class="ruby-identifier">to</span> <span class="ruby-identifier">eq</span>(<span class="ruby-string">&quot;&quot;</span>)
</pre>

<p>The one known case this doesn’t handle is:</p>

<pre class="ruby"><span class="ruby-constant">Ripper</span>.<span class="ruby-identifier">lex</span> <span class="ruby-identifier">&lt;&lt;~EOM</span>
<span class="ruby-value">  a &amp;&amp;
   b ||
   c
</span><span class="ruby-identifier">EOM</span>
</pre>

<p>For some reason this introduces ‘on_ignore_newline` but with BEG type</p>

                              <div class="method-source-code" id="join_consecutive-21-source">
            <pre><span class="ruby-comment"># File syntax_suggest/clean_document.rb, line 225</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">join_consecutive!</span>
  <span class="ruby-identifier">consecutive_groups</span> = <span class="ruby-ivar">@document</span>.<span class="ruby-identifier">select</span>(<span class="ruby-operator">&amp;</span><span class="ruby-value">:ignore_newline_not_beg?</span>).<span class="ruby-identifier">map</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">code_line</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">take_while_including</span>(<span class="ruby-identifier">code_line</span>.<span class="ruby-identifier">index</span><span class="ruby-operator">..</span><span class="ruby-value">-1</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">line</span><span class="ruby-operator">|</span>
      <span class="ruby-identifier">line</span>.<span class="ruby-identifier">ignore_newline_not_beg?</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">join_groups</span>(<span class="ruby-identifier">consecutive_groups</span>)
  <span class="ruby-keyword">self</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-join_groups" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">join_groups</span><span
                                class="method-args">(groups)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Helper method for joining “groups” of lines</p>

<p>Input is expected to be type Array&lt;Array&lt;CodeLine&gt;&gt;</p>

<p>The outer array holds the various “groups” while the inner array holds code lines.</p>

<p>All code lines are “joined” into the first line in their group.</p>

<p>To preserve document size, empty lines are placed in the place of the lines that were “joined”</p>

                              <div class="method-source-code" id="join_groups-source">
            <pre><span class="ruby-comment"># File syntax_suggest/clean_document.rb, line 266</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">join_groups</span>(<span class="ruby-identifier">groups</span>)
  <span class="ruby-identifier">groups</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">lines</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">line</span> = <span class="ruby-identifier">lines</span>.<span class="ruby-identifier">first</span>

    <span class="ruby-comment"># Handle the case of multiple groups in a a row</span>
    <span class="ruby-comment"># if one is already replaced, move on</span>
    <span class="ruby-keyword">next</span> <span class="ruby-keyword">if</span> <span class="ruby-ivar">@document</span>[<span class="ruby-identifier">line</span>.<span class="ruby-identifier">index</span>].<span class="ruby-identifier">empty?</span>

    <span class="ruby-comment"># Join group into the first line</span>
    <span class="ruby-ivar">@document</span>[<span class="ruby-identifier">line</span>.<span class="ruby-identifier">index</span>] = <span class="ruby-constant">CodeLine</span>.<span class="ruby-identifier">new</span>(
      <span class="ruby-value">lex:</span> <span class="ruby-identifier">lines</span>.<span class="ruby-identifier">map</span>(<span class="ruby-operator">&amp;</span><span class="ruby-value">:lex</span>).<span class="ruby-identifier">flatten</span>,
      <span class="ruby-value">line:</span> <span class="ruby-identifier">lines</span>.<span class="ruby-identifier">join</span>,
      <span class="ruby-value">index:</span> <span class="ruby-identifier">line</span>.<span class="ruby-identifier">index</span>
    )

    <span class="ruby-comment"># Hide the rest of the lines</span>
    <span class="ruby-identifier">lines</span>[<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">-1</span>].<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">line</span><span class="ruby-operator">|</span>
      <span class="ruby-comment"># The above lines already have newlines in them, if add more</span>
      <span class="ruby-comment"># then there will be double newline, use an empty line instead</span>
      <span class="ruby-ivar">@document</span>[<span class="ruby-identifier">line</span>.<span class="ruby-identifier">index</span>] = <span class="ruby-constant">CodeLine</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">line:</span> <span class="ruby-string">&quot;&quot;</span>, <span class="ruby-value">index:</span> <span class="ruby-identifier">line</span>.<span class="ruby-identifier">index</span>, <span class="ruby-value">lex:</span> [])
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">self</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-join_heredoc-21" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">join_heredoc!</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Smushes all heredoc lines into one line</p>

<pre class="ruby"><span class="ruby-identifier">source</span> = <span class="ruby-identifier">&lt;&lt;~&#39;EOM&#39;</span>
<span class="ruby-value">  foo = &lt;&lt;~HEREDOC
     lol
     hehehe
  HEREDOC
</span><span class="ruby-identifier">EOM</span>

<span class="ruby-identifier">lines</span> = <span class="ruby-constant">CleanDocument</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">source:</span> <span class="ruby-identifier">source</span>).<span class="ruby-identifier">join_heredoc!</span>.<span class="ruby-identifier">lines</span>
<span class="ruby-identifier">expect</span>(<span class="ruby-identifier">lines</span>[<span class="ruby-value">0</span>].<span class="ruby-identifier">to_s</span>).<span class="ruby-identifier">to</span> <span class="ruby-identifier">eq</span>(<span class="ruby-identifier">source</span>)
<span class="ruby-identifier">expect</span>(<span class="ruby-identifier">lines</span>[<span class="ruby-value">1</span>].<span class="ruby-identifier">to_s</span>).<span class="ruby-identifier">to</span> <span class="ruby-identifier">eq</span>(<span class="ruby-string">&quot;&quot;</span>)
</pre>

                              <div class="method-source-code" id="join_heredoc-21-source">
            <pre><span class="ruby-comment"># File syntax_suggest/clean_document.rb, line 181</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">join_heredoc!</span>
  <span class="ruby-identifier">start_index_stack</span> = []
  <span class="ruby-identifier">heredoc_beg_end_index</span> = []
  <span class="ruby-identifier">lines</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">line</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">line</span>.<span class="ruby-identifier">lex</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">lex_value</span><span class="ruby-operator">|</span>
      <span class="ruby-keyword">case</span> <span class="ruby-identifier">lex_value</span>.<span class="ruby-identifier">type</span>
      <span class="ruby-keyword">when</span> <span class="ruby-value">:on_heredoc_beg</span>
        <span class="ruby-identifier">start_index_stack</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">line</span>.<span class="ruby-identifier">index</span>
      <span class="ruby-keyword">when</span> <span class="ruby-value">:on_heredoc_end</span>
        <span class="ruby-identifier">start_index</span> = <span class="ruby-identifier">start_index_stack</span>.<span class="ruby-identifier">pop</span>
        <span class="ruby-identifier">end_index</span> = <span class="ruby-identifier">line</span>.<span class="ruby-identifier">index</span>
        <span class="ruby-identifier">heredoc_beg_end_index</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-identifier">start_index</span>, <span class="ruby-identifier">end_index</span>]
      <span class="ruby-keyword">end</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">heredoc_groups</span> = <span class="ruby-identifier">heredoc_beg_end_index</span>.<span class="ruby-identifier">map</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">start_index</span>, <span class="ruby-identifier">end_index</span><span class="ruby-operator">|</span> <span class="ruby-ivar">@document</span>[<span class="ruby-identifier">start_index</span><span class="ruby-operator">..</span><span class="ruby-identifier">end_index</span>] }

  <span class="ruby-identifier">join_groups</span>(<span class="ruby-identifier">heredoc_groups</span>)
  <span class="ruby-keyword">self</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-join_trailing_slash-21" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">join_trailing_slash!</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Join lines with a trailing slash</p>

<pre class="ruby"><span class="ruby-identifier">source</span> = <span class="ruby-identifier">&lt;&lt;~&#39;EOM&#39;</span>
<span class="ruby-value">  it &quot;code can be split&quot; \
     &quot;across multiple lines&quot; do
</span><span class="ruby-identifier">EOM</span>

<span class="ruby-identifier">lines</span> = <span class="ruby-constant">CleanDocument</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">source:</span> <span class="ruby-identifier">source</span>).<span class="ruby-identifier">join_consecutive!</span>.<span class="ruby-identifier">lines</span>
<span class="ruby-identifier">expect</span>(<span class="ruby-identifier">lines</span>[<span class="ruby-value">0</span>].<span class="ruby-identifier">to_s</span>).<span class="ruby-identifier">to</span> <span class="ruby-identifier">eq</span>(<span class="ruby-identifier">source</span>)
<span class="ruby-identifier">expect</span>(<span class="ruby-identifier">lines</span>[<span class="ruby-value">1</span>].<span class="ruby-identifier">to_s</span>).<span class="ruby-identifier">to</span> <span class="ruby-identifier">eq</span>(<span class="ruby-string">&quot;&quot;</span>)
</pre>

                              <div class="method-source-code" id="join_trailing_slash-21-source">
            <pre><span class="ruby-comment"># File syntax_suggest/clean_document.rb, line 246</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">join_trailing_slash!</span>
  <span class="ruby-identifier">trailing_groups</span> = <span class="ruby-ivar">@document</span>.<span class="ruby-identifier">select</span>(<span class="ruby-operator">&amp;</span><span class="ruby-value">:trailing_slash?</span>).<span class="ruby-identifier">map</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">code_line</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">take_while_including</span>(<span class="ruby-identifier">code_line</span>.<span class="ruby-identifier">index</span><span class="ruby-operator">..</span><span class="ruby-value">-1</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">x</span><span class="ruby-operator">|</span> <span class="ruby-identifier">x</span>.<span class="ruby-identifier">trailing_slash?</span> }
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">join_groups</span>(<span class="ruby-identifier">trailing_groups</span>)
  <span class="ruby-keyword">self</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-lines" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">lines</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Return an array of CodeLines in the document</p>

                              <div class="method-source-code" id="lines-source">
            <pre><span class="ruby-comment"># File syntax_suggest/clean_document.rb, line 104</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">lines</span>
  <span class="ruby-ivar">@document</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-take_while_including" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">take_while_including</span><span
                                class="method-args">(range = 0..-1) { |line)| ... }</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Helper method for grabbing elements from document</p>

<p>Like ‘take_while` except when it stops iterating, it also returns the line that caused it to stop</p>

                              <div class="method-source-code" id="take_while_including-source">
            <pre><span class="ruby-comment"># File syntax_suggest/clean_document.rb, line 296</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">take_while_including</span>(<span class="ruby-identifier">range</span> = <span class="ruby-value">0</span><span class="ruby-operator">..</span><span class="ruby-value">-1</span>)
  <span class="ruby-identifier">take_next_and_stop</span> = <span class="ruby-keyword">false</span>
  <span class="ruby-ivar">@document</span>[<span class="ruby-identifier">range</span>].<span class="ruby-identifier">take_while</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">line</span><span class="ruby-operator">|</span>
    <span class="ruby-keyword">next</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">take_next_and_stop</span>

    <span class="ruby-identifier">take_next_and_stop</span> = <span class="ruby-operator">!</span>(<span class="ruby-keyword">yield</span> <span class="ruby-identifier">line</span>)
    <span class="ruby-keyword">true</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-to_s" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">to_s</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Renders the document back to a string</p>

                              <div class="method-source-code" id="to_s-source">
            <pre><span class="ruby-comment"># File syntax_suggest/clean_document.rb, line 109</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">to_s</span>
  <span class="ruby-ivar">@document</span>.<span class="ruby-identifier">join</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                          </section>

              </section>
              </main>



            </div>  <!--  class='wrapper hdiv' -->


<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a></p>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.4.0.</p>
<p>Based on <a href="https://github.com/ged/darkfish/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.</p>

  
    <p><p><a href="https://ruby-doc.org">Ruby-doc.org</a> is provided by <a href="https://jamesbritt.com">James Britt</a> and <a href="https://neurogami.com">Neurogami</a>.</p><p><a href="https://jamesbritt.bandcamp.com/">Maximum R+D</a>.  </p>
</p>
  
  </footer>

<script type="text/javascript">


  let ads  = $("#carbonads-container").children().detach();


  function swapMode() {
    var cookieName = 'darkmode';
    var cssDarkmode = Cookies.get(cookieName);
    console.log("***** swapMode! " + cssDarkmode + " *****");


    if (cssDarkmode == "true") {
      console.log("We have dark mode, set the css to light ...");
      $('#rdoccss').attr("href", defaultModeCssHref);
      $('#cssSelect').text("Dark mode");
      cssDarkmode = "false";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    } else {
      console.log("We not have dark mode, set the css to dark ...");
      $('#rdoccss').attr("href", darkModeCsseHref);
      $('#cssSelect').text("Light mode");
      cssDarkmode = "true";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    }

    console.log("  --------------- ");
  }


const vueCssApp = new Vue({
el: '#menubar',
data: {
isDark: false
},
methods: {
toggleClass: function(event){
this.isDark = !this.isDark;
}
}
})

const vueApp = new Vue({
el: '#vapp',
data: { 
isOpen: true
},

mounted() {
this.handleResize();
this.manage_mob_classes();
window.addEventListener('resize', this.handleResize)
//this.isOpen !=  (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},
destroyed() {
window.removeEventListener('resize', this.handleResize)
},
created() {
//manage_mob_classes();
},

methods : {
isMobile() {
  return (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},

  handleResize() {
    if (!this.isMobile()) {
      this.isOpen = window.innerWidth > 800;
    }
  },

  manage_mob_classes() {
    if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
      $("nav").addClass("mob_nav");
      $("main").addClass("mob_main");
      $("#extraz").addClass("mob_extraz");
      $("#carbonads-container").addClass("mob_carbonads-container");
      this.isOpen  = false;
    } else {
      $("nav").removeClass("mob_nav") 
        $("main").removeClass("mob_main");
      $("#extraz").removeClass("mob_extraz");
      $("#carbonads-container").removeClass("mob_carbonads-container");
      this.isOpen  = true;
    }
  },

  toggleNav() {
    this.isOpen =! this.isOpen ;
    // alert("Toggle nav!");
    console.log("toggleNav() click: " + this.isOpen );
  }
}
})

$("#carbonads-container").append(ads);


$(function() {

    var darkmode = Cookies.get("darkmode");
    console.log("Document ready: " + darkmode);

    if ( darkmode  == "true" ) {
      $('#cssSelect').text("Light mode");
    } else {
      $('#cssSelect').text("Dark mode");
     }

    $('body').css('display','block');
    });

</script>

    
  </body> 
</html>

